import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 19182
 * Date: 2025-09-02
 * Time: 21:30
 */
public class MyThreadPool {
    BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
    public MyThreadPool(int n){
        for(int i = 0; i < n; i++){
            Thread t = new Thread(()->{
                while(true){
                    try {
                        Runnable task = queue.take();
                        task.run();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
            t.setDaemon(true);
            t.start();
        }
    }
    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }
        public static void main(String[] args) throws InterruptedException {
            MyThreadPool myThreadPool = new MyThreadPool(10);
            for(int i = 0; i < 1000; i++){
                final int id = i;
                myThreadPool.submit(()->{
                    Thread cur = Thread.currentThread();
                    System.out.println(cur.getName() + "," + id);
                });
            }
            Thread.sleep(1000); //让主线程等一下线程池中10个线程把任务执行完, 主线程是前台线程, 如果主线程先结束了, 那么后台线程就也结束
    }



}
